<template>
  <div class="login-container">
    <div class="background">
      <img src="../assets/img/back.png" width="100%" alt="" />
    </div>
    <div class="login-box">
      <!-- <div class="title-icon">
        <img src="../assets/images/school.png" alt="" />
      </div> -->
      <section>
        <h1 class="titleH1">智能化高校课堂教学评估系统</h1>
      </section>

      <!-- 使用element-ui来进行操作，用户名和密码表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login-input"
        @keydown.enter.native="login"
      >
        <el-form-item prop="userName">
          <el-input
            placeholder="账号"
            v-model="loginForm.userName"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 验证码输入框 -->
        <el-form-item prop="code">
          <el-input
            class="inputVerification"
            placeholder="验证码"
            v-model="value2Computed"
            autocomplete="off"
          ></el-input>
          <!-- 验证码图片 -->
          <div>
            <span class="demonstration"></span>
            <el-image
              :src="src"
              class="imageVer"
              @click="refreshPicture"
              title="点击刷新验证码"
            >
              <div slot="placeholder" class="image-slot">
                <span class="dot"></span>
              </div>
            </el-image>
          </div>
        </el-form-item>

        <!-- 登录和重置按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import vercode from '../api/global.js'
export default {
  data() {
    return {
      imgSrc:
        'https://cn.bing.com//th?id=OHR.HuntsMesa_JA-JP3140979616_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp',
      // verCode: '',
      loginForm: {
        userName: 'admin',
        password: '123456',
        code: ''
      },
      loginFormRules: {
        userName: [
          //  trigger:'blur'鼠标失去焦点时触发，change表示数据改变时触发
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '请输入验证码',
            min: 0,
            trigger: 'blur'
          }
        ]
      },
      src: ''
    }
  },
  methods: {
    resetLoginForm() {
      // console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    // 用户登录信息判断
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        console.log('vercode', vercode.verCode)
        console.log('code', this.loginForm.code)
        if (!this.loginForm.code) {
          return
        }
        if (vercode.verCode === this.loginForm.code) {
          console.log('验证码正确')
        } else {
          this.$message.error('验证码错误')
          return
        }
        // 输出用户输入信息是否正确
        if (!valid) return
        const requestLoginMessage = await this.$axios.post(
          '/api/login/validate',
          {
            username: this.loginForm.userName,
            password: this.loginForm.password
          }
        )
        console.log(requestLoginMessage)
        if (requestLoginMessage.data.id) {
          console.log(requestLoginMessage)
          window.sessionStorage.setItem(
            'id',
            requestLoginMessage.data.id
          )
          this.$message.success('登录成功')
          this.$router.push('/home')
          return
        }
        this.$message.error('登录失败,用户名或密码错误')
      })
    },
    // 点击刷新图形验证码
    refreshPicture() {
      var num = Math.ceil(Math.random() * 10) // 生成一个随机数（防止缓存)
      const strSrc = '/api/login/validateCode?' + num
      this.$axios
        .get(strSrc, {
          responseType: 'arraybuffer'
        })
        .then((data) => {
          console.log('接收到的验证码是：', data.headers.validatecode)
          // 定义全局变量来记录验证码的数值
          vercode.verCode = data.headers.validatecode
          this.src =
            'data:image/png;base64,' +
            btoa(
              new Uint8Array(data.data).reduce(
                (data, byte) => data + String.fromCharCode(byte),
                ''
              )
            )
        })
    }
  },
  computed: {
    value2Computed: {
      get: function() {
        return this.loginForm.code
      },
      set: function(val) {
        this.loginForm.code = val.toUpperCase()
      }
    }
  },
  mounted() {
    this.$axios
      .get('/api/login/validateCode', {
        responseType: 'arraybuffer'
      })
      .then((data) => {
        console.log('接收到的验证码是：', data.headers.validatecode)
        // 定义全局变量来记录验证码的数值
        vercode.verCode = data.headers.validatecode
        this.src =
          'data:image/png;base64,' +
          btoa(
            new Uint8Array(data.data).reduce(
              (data, byte) => data + String.fromCharCode(byte),
              ''
            )
          )
      })
  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  background-color: rgba(0, 0, 0, 0.2);
  .background {
    height: 100%;
    img {
      height: 100%;
    }
  }
}

.login-box {
  height: 450px;
  width: 450px;
  border-radius: 3px;
  background-color: #fff;
  position: absolute;
  border-radius: 10px;
  box-shadow: 10px 2px 52px rgb(206, 199, 186);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  section {
    height: 50px;
    text-align: center;
    margin-top: 15px;
  }
  h1 {
    font-size: 2rem;
    color:rgb(49, 176, 230)
  }
}

.title-icon {
  height: 100px;
  width: 100%;

  img {
    height: 80px;
    width: 280px;
    position: absolute;
    transform: translate(85px, 25px);
  }
}

.login-input {
  outline: none;
  position: absolute;
  top: 180px;
  bottom: 0;
  width: 100%;
  padding: 0px 50px;
  box-sizing: border-box;
}

.btn {
  display: flex;
  justify-content: flex-end;
  padding: 0 10px;
}

.el-button + .el-button {
  margin-left: 30px;
}

.inputVerification {
  width: 150px;
}

.imageVer {
  height: 40px;
  width: 120px;
  float: right;
  position: absolute;
  transform: translate(200px, -40px);
}
.titleH1{
  margin-top: 60px;
  text-align: center;
}
</style>
